package com.example.orderflow.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSON;
import com.example.orderflow.entity.constants.CommonConstans;
import org.springframework.stereotype.Service;

import com.example.orderflow.entity.enums.PageSize;
import com.example.orderflow.entity.query.OrderEquipmentQuery;
import com.example.orderflow.entity.po.OrderEquipment;
import com.example.orderflow.entity.vo.PaginationResultVO;
import com.example.orderflow.entity.query.SimplePage;
import com.example.orderflow.mappers.OrderEquipmentMapper;
import com.example.orderflow.service.OrderEquipmentService;
import com.example.orderflow.utils.StringTools;


/**
 *  业务接口实现
 */
@Service("orderEquipmentService")
public class OrderEquipmentServiceImpl implements OrderEquipmentService {

	@Resource
	private OrderEquipmentMapper<OrderEquipment, OrderEquipmentQuery> orderEquipmentMapper;

	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<OrderEquipment> findListByParam(OrderEquipmentQuery param) {
		return this.orderEquipmentMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(OrderEquipmentQuery param) {
		return this.orderEquipmentMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<OrderEquipment> findListByPage(OrderEquipmentQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<OrderEquipment> list = this.findListByParam(param);
		PaginationResultVO<OrderEquipment> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(OrderEquipment bean) {
		return this.orderEquipmentMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<OrderEquipment> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.orderEquipmentMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<OrderEquipment> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.orderEquipmentMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(OrderEquipment bean, OrderEquipmentQuery param) {
		StringTools.checkParam(param);
		return this.orderEquipmentMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(OrderEquipmentQuery param) {
		StringTools.checkParam(param);
		return this.orderEquipmentMapper.deleteByParam(param);
	}

	/**
	 * 根据Id获取对象
	 */
	@Override
	public OrderEquipment getOrderEquipmentById(Integer id) {
		return this.orderEquipmentMapper.selectById(id);
	}

	/**
	 * 根据Id修改
	 */
	@Override
	public Integer updateOrderEquipmentById(OrderEquipment bean, Integer id) {
		return this.orderEquipmentMapper.updateById(bean, id);
	}

	/**
	 * 根据Id删除
	 */
	@Override
	public Integer deleteOrderEquipmentById(Integer id) {
		return this.orderEquipmentMapper.deleteById(id);
	}

	/**
	 * 根据orderId查询对象
	 */
	@Override
	public List<OrderEquipment> selectByOrderId(String orderId){
		return this.orderEquipmentMapper.selectByOrderId(orderId);
	};

	/**
	 * 添加订单设备
	 */
	@Override
	public void addOrderEquipment(String orderId,String equipmentTypeSpectrum,Object otherParameters,
						   String quantity,String notes){

		Date day = new Date();
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		OrderEquipment orderEquipment = new OrderEquipment();

		orderEquipment.setOrderId(orderId);
		orderEquipment.setEquipmentTypeSpectrum(equipmentTypeSpectrum);
		orderEquipment.setOtherParameters(JSON.toJSONString(otherParameters));
		orderEquipment.setQuantity(quantity);
		orderEquipment.setNotes(notes);
		orderEquipment.setCreationTime(sdf.format(day));
		orderEquipment.setUpdateTime(sdf.format(day));
		orderEquipment.setOpinionOfSupplySupervisor(CommonConstans.DEFAULT);
		orderEquipment.setOpinionsOfChiefProductionEngineer(CommonConstans.DEFAULT);

		add(orderEquipment);

		return;

	};
}